class ArticlesController < ApplicationController
  before_action :set_create_article, only: [:show, :edit, :update, :destroy]
  before_action :set_topic, only: [:new]
  before_action :set_article, only: [:new, :top]
  before_action :authenticate_user!, only: [:new, :edit, :update, :destroy]

  def index
    @title = @article.title
    @articles = Article.all
  end

  # GET /articles/1
  # GET /articles/1.json
  def show
    @title = @article.title
    @topic = @article.topic
    # @attachment = @article.attachments
    update_click
  end

  # GET /articles/new
  def new
    @title = '添加文章'
  end

  # GET /articles/1/edit
  def edit
    @title = '编辑文章'
  end

  # POST /articles
  # POST /articles.json
  # 没用到，已直接先保存了。
  def create
    @article = Article.new(create_article_params)
    @topic = @article.topic
    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: '文章已保存.' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    respond_to do |format|
      if @article.update(create_article_params)
        # 检测文章中的图片是否还在使用（通过查看文章内容中有没有某个图片地址）
        # 如果没有，则把此图片标记为未使用，以便在文章列表中获取图片
        # article.attachments.where(asset_type: 'image', is_used: true).limit(1)
        # TODO view层只获取了当前文章的图片，如果是非本文章所有的图片则不会在列表中显示
        @article.attachments.where(asset_type: 'image').each do |image|
          if @article.content.include?(image.asset.to_s)
            image.update(is_used: true)
          else
            image.update(is_used: false)
          end
        end
        format.html { redirect_to @article, notice: '文章已保存.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    @topic = @article.topic
    @article.update(is_deleted: true)
    @article.attachments.each do |att|
      att.update(is_used: false)
    end
    respond_to do |format|
      format.html { redirect_to topic_path(@topic), notice: '文章删除成功！' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_create_article
    @article = Article.unscoped.load.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def create_article_params
    params[:article].merge(is_published: true)\
                    .permit(:title, :content, :topic_id, :user_id, :score, :is_published, :is_top)
  end

  def set_topic
    @topic = Topic.find(params[:id])
  end

  def update_click
    @article.update_attributes(click: @article.click+1)
  end

  def get_article
    @article = Article.find
  end

  def set_article
    @article = Article.unscoped.where(is_published: false, topic_id: @topic.id).first
    if @article
      #pass
    else
      save_template_article!
    end
    clean_template_article!
  end

  def save_template_article!
    @article = Article.new
    title = '请输入标题'
    content = '请输入内容'
    user_id = current_user.id
    topic_id = @topic.id
    @article.update_attributes(title: title,
                               content: content,
                               user_id: user_id,
                               topic_id: topic_id,
                               is_published: false)
  end

  def clean_template_article!
    @article.update_attributes(created_at: Time.now,
                               updated_at: Time.now,
                               is_published: false)
    @article.title = ''
    @article.content = ''
  end

end
